package com.lanhai.zhonglai.dispatch;
 
import java.util.Arrays;
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.dao.RoadDao;
import com.lanhai.zhonglai.dao.StationGroupDao;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.CacheStationEnumType;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.ICmdLog;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.third.StationFactory;
 

/**
 * 
 * 二维码离开的判断(未传输完成就离开的）
 * 
 * @author zhengwei
 *
 */
@Component
public class QrLeaveCheck {
	@Autowired
	IStation iStation;

	@Autowired
	IAgvEvent iAgvEvent;

	@Autowired
	ICmdLog iCmdLog;

	@Autowired
	IDot iDot;

	@Autowired
	IAgv iAgv;

	@Autowired
	IRoad iRoad;
	
	@Autowired
	ISendData sendTools;

 
 
	/**
	 * 返回是否处理
	 * 
	 * @param agv
	 * @param dot
	 * @param amount
	 * @return
	 */
	public void check(AgvBean agv, DotBean dot, AgvMountBean amount, int transDotno) {
		StationBean station = iStation.getBeanByDot(transDotno);
		if (station == null) {
			return;
		}
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());
		byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
		if (station.getCmdSign().equals("HC")) {
			sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
					station.getStationname());
		} else {
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
					station.getStationname());
		}
		
		
		// 带料情况下各二维码的判断
		int qrdot = dot.getDotno();
		if (qrdot == 1 || qrdot == 3 || qrdot == 5 || qrdot == 7
				||qrdot==217||qrdot==215||qrdot==213||qrdot==211||qrdot==209
				||qrdot==207||qrdot==203||qrdot==205) {
			// 制绒上料
			leaveZRS(agv, amount, station, dot);
		} else if (qrdot == 9 || qrdot == 11 || qrdot == 13 || qrdot == 15) {
			// 制绒下料
			leaveZRX(agv, amount, station, dot);
		} else if (qrdot == 17 || qrdot == 21 || qrdot == 27 || qrdot == 29 || qrdot == 35 || qrdot == 37) {
			// 扩散上料
			leaveKSS(agv, amount, station, dot);
		} else if (qrdot == 19 || qrdot == 23 || qrdot == 25 || qrdot == 31 || qrdot == 33 || qrdot == 39) {
			// 扩散下料
			leaveKSX(agv, amount, station, dot);
		} else if (qrdot == 57 || qrdot == 59 || qrdot == 61 || qrdot == 63) {
			// BSG
			leaveBSG(agv, amount, station, dot);
		} else if (qrdot == 73 || qrdot == 75 || qrdot == 77 || qrdot == 79) {
			// 碱抛上花篮
			leaveJPS(agv, amount, station, dot);
		} else if (qrdot == 49 || qrdot == 51 || qrdot == 53 || qrdot == 55) {
			// 碱抛下料
			leaveJPX(agv, amount, station, dot);
		} else if (qrdot == 41 || qrdot == 43 || qrdot == 45 || qrdot == 47) {
			// POPAID
			leavePOPAID(agv, amount, station, dot);

		} else if (qrdot == 89 || qrdot == 93 || qrdot == 97 || qrdot == 101) {
			// 退火上
			leaveTHS(agv, amount, station, dot);
		} else if (qrdot == 91 || qrdot == 95 || qrdot == 99 || qrdot == 103) {
			// 退火下
			leaveTHX(agv, amount, station, dot);

		} else if (qrdot == 65 || qrdot == 67 || qrdot == 69 || qrdot == 71) {
			// BOE上
			leaveBOES(agv, amount, station, dot);
		} else if (qrdot == 81 || qrdot == 83 || qrdot == 85 || qrdot == 87) {
			// BOE下
			leaveBOEX(agv, amount, station, dot);

		} else if (qrdot == 199 || qrdot == 197 || qrdot == 195 || qrdot == 193 || qrdot == 191 || qrdot == 189
				|| qrdot == 187 || qrdot == 185) {
			// 丝网
			leaveSW(agv, amount, station, dot);
		} else if (qrdot == 121 || qrdot == 127 || qrdot == 129 || qrdot == 135 || qrdot == 137 || qrdot == 143
				|| qrdot == 145) {
			// 正膜上料
			leaveZMS(agv, amount, station, dot);
		} else if (qrdot == 123 || qrdot == 125 || qrdot == 131 || qrdot == 133 || qrdot == 139 || qrdot == 141
				|| qrdot == 147) {
			// 正膜下料
			leaveZMX(agv, amount, station, dot);
		} else if (qrdot == 105 || qrdot == 111 || qrdot == 113 || qrdot == 119 || qrdot == 151 || qrdot == 153
				|| qrdot == 159) {
			// 背膜上料
			leaveBMS(agv, amount, station, dot);
		} else if (qrdot == 107 || qrdot == 109 || qrdot == 115 || qrdot == 117 || qrdot == 149 || qrdot == 155
				|| qrdot == 157) {
			// 背膜下料
			leaveBMX(agv, amount, station, dot);
		} else if (qrdot == 161 || qrdot == 163 || qrdot == 169 || qrdot == 171 || qrdot == 177 || qrdot == 179) {
			// ALD干
			leaveALDG(agv, amount, station, dot);
		} else if (qrdot == 165 || qrdot == 167 || qrdot == 173 || qrdot == 175 || qrdot == 181 || qrdot == 183) {
			// ALD湿法
			leaveALDS(agv, amount, station, dot);
		}else if(qrdot==201) {
			leaveCB(agv, amount, station, dot); 
		}

	}

	/**
	 * 只走路由路线，AGV不锁任何机台但会解锁二维码对应的机台
	 * 
	 * @param agv
	 * @param dot
	 * @param transdot
	 */
	void toRoute(AgvBean agv, DotBean dot, int transdot,int specialline,String process,String stationname,boolean isHC) {
		// 重置机台传输数据
		StationCmdMap.clearState(transdot);
		// 空车解锁所有的
		iStation.unlockStationForDot(transdot, agv.getAgvId()); 
		
		// 行驶
		agv.setNextProcess(process);
		agv.setNexttStationName(stationname);
		agv.setNextRoad(0);
		agv.setSpecialline(specialline);
		agv.setFinishTrans(false);
		agv.setUpHasTrans(false);
		agv.setDownHasTrans(false);
		agv.setToHC(isHC);
		iAgv.saveCache(agv);

		
		if (stationname!=null&&stationname.equals("")==false) {
			if(specialline>0) {
				MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + stationname+",生产线："+specialline);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + stationname);
			}
			
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "去下一道工序");
		}
		
		
		// 只走点位路由
		if (dot.getRouteRoad() > 0) {
			// 小车跑路
			iAgvEvent.toRoad(agv, dot.getRouteRoad());
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "二维码数据错误，缺少目标路线");
		}
	}
 
	/**
	 * 只走路由路线，AGV不锁任何机台但会解锁二维码对应的机台
	 * 
	 * @param agv
	 * @param dot
	 * @param transdot
	 */
	void toDot(AgvBean agv, int road, int transdot,int specialline,String process,String stationname,boolean isHC) {
		// 重置机台传输数据
		StationCmdMap.clearState(transdot);
		// 空车解锁所有的
		iStation.unlockStationForDot(transdot, agv.getAgvId()); 
		
		// 行驶
		agv.setNextProcess(process);
		agv.setNexttStationName(stationname);
		agv.setNextRoad(0);
		agv.setSpecialline(specialline);
		agv.setFinishTrans(false);
		agv.setUpHasTrans(false);
		agv.setDownHasTrans(false);
		agv.setToHC(isHC);
		iAgv.saveCache(agv);

		
		if (stationname!=null&&stationname.equals("")==false) {
			if(specialline>0) {
				MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + stationname+",生产线："+specialline);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + stationname);
			}
			
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "去下一道工序");
		}
		iAgvEvent.toRoad(agv, road);
		 
	}
	 
	/**
	 * 先离开二维码，然后再走特定路线
	 * 如果没有特定路线，则走外部点的路由
	 * @param agv
	 * @param dot
	 * @param transdot
	 * @param process
	 * @param stationname
	 * @param specialline
	 * @param nextroad
	 */
	void toStation(AgvBean agv, DotBean dot, int transdot, String process, String stationname,
			int specialline,int nextroad) {
		// 重置机台传输数据
		StationCmdMap.clearState(transdot);
		 
		iStation.unlockStationForDot(transdot, agv.getAgvId());

		if (stationname.equals("")==false) {
			if(specialline>0) {
				MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + stationname+",生产线："+specialline);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "目标机台：" + stationname);
			}
			
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "去下一道工序");
		}
		// 行驶
		agv.setNextProcess(process);
		agv.setNexttStationName(stationname);
		agv.setFinishTrans(false);
		agv.setUpHasTrans(false);
		agv.setDownHasTrans(false); 
		agv.setNextRoad(nextroad); 
		agv.setSpecialline(specialline);
		agv.setToHC(false);
		iAgv.saveCache(agv);

		if (dot.getRouteRoad() > 0) {
			// 小车跑路
			iAgvEvent.toRoad(agv, dot.getRouteRoad());
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "数据错误，缺少目标路线");
		}
	}

	// ********************** 各工序判断 ***************

	/**
	 * 制绒上料，发料小车和普通车
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 */
	void leaveZRS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) { 
		if (agv.getAgvId() == 19 || agv.getAgvId() == 20) {
			if (amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
				// 发料车上空下满，完成传输
			 
				//没有特殊路线，也没有定线
				// 重置机台传输数据
				StationCmdMap.clearState(station.getDotno());
				// 空车解锁所有的
				iStation.unlockStationForDot(station.getDotno(), agv.getAgvId());
 
				// 行驶
				agv.setTransing(false);
				agv.setNextProcess("");
				agv.setNexttStationName("");
				agv.setNextRoad(0);
				agv.setSpecialline(0);
				agv.setFinishTrans(false);
				agv.setUpHasTrans(false);
				agv.setDownHasTrans(false); 
				iAgv.saveCache(agv);
     
				int road=iRoad.getNormalRoad(dot.getDotno(), 363);
				if(road>0) {
					iAgvEvent.toRoad(agv, road);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), "缺少到363的路线");
				}
				
			} else {
				MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
			}
		} else {
			// 普通车
			if (amount.getMountType() != AgvMountTypeEnum.AllEmpty) {
				MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
			} else {
				 
				//没有特殊路线，也没有定线
				toRoute(agv, dot, station.getDotno(),0,"制绒下料","",false);
			}
		}
	}

	/**
	 * 制绒下料，全空去路由，上满下空才能走 
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 */
	void leaveZRX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车，没有做过传输
			toRoute(agv, dot, station.getDotno(),0,"制绒下料","",false);
			return;
		}

		if (amount.getMountType() != AgvMountTypeEnum.UpEmptyAndDownFull) {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
			return;
		}
		 
		// 走路由去406做判断
		String nextProcess = "扩散上料";
		StationGroupDao gbean =iStation.fetchNextInStation(406, agv.getAgvId(), nextProcess, station.getSpecialline(), true);
		
		if (gbean == null) {
			// 没有机台，找制绒缓存机
			boolean tocache=iStation.findCache(CacheStationEnumType.ZRX	,agv.getAgvId(), true,true );
			if(tocache) {
				//可以去缓存 
				//去406
				toRoute(agv, dot, station.getDotno(), station.getSpecialline(),nextProcess  ,CacheStationEnumType.ZRX.getName(),true); 
				
			}else {
				MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
			} 
		} else {
			// 不锁机台，只是去406
			//(gbean.getDotno(), agv.getAgvId(),false);
			toRoute(agv, dot, station.getDotno(), station.getSpecialline(), gbean.getProcess(), gbean.getStationname(),false); 
		}

	}

	/**
	 * 扩散上料后就是空车。寻找扩散下料，如果有则去，没有则路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveKSS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() != AgvMountTypeEnum.AllEmpty) {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		} else {
		 

			String nextProcess = "扩散下料";
			// 从406开始找
			StationGroupDao gbean =iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);

			if (gbean == null) {
				// 走空车
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			} else { 
				RoadDao road = iRoad.getTransRoad(406, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					//有特殊路线，空车定线  
					toStation  (agv, dot, station.getDotno(),nextProcess, gbean.getStationname(),gbean.getSpecialline(),road.getId());
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406,gbean.getDotno())); 
				} 
			}

		}
	}

	/**
	 * 扩散下料，直接走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveKSX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车，没有传输,，走路由
			toRoute(agv, dot, station.getDotno(),0,"","",false);
		} else if (amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
			// 出料,上空下满
			 
			// 找BSG 
			String nextProcess = "BSG";  
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
			
			if (gbean == null) {
				// 装料车需要等
				boolean tocache=iStation.findCache(CacheStationEnumType.KS	,agv.getAgvId(), true,true);
				if(tocache) {
					//可以去缓存
					RoadDao road = iRoad.getTransRoad(406, CacheStationEnumType.KS.getDotno());
					if (road != null && road.getId() > 0) { 
						//有特殊路线，空车定线  
						toStation(agv, dot, station.getDotno(),nextProcess, CacheStationEnumType.KS.getName(),station.getSpecialline(),road.getId());
					} else {
						MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406,CacheStationEnumType.KS.getDotno())); 
					}   
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
				}  
			} else { 
				toRoute(agv, dot, station.getDotno(),gbean.getSpecialline(),gbean.getProcess() ,gbean.getStationname(),false);
			}
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	/**
	 * BSG，只要上面满或者全空，就走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveBSG(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 上完料，或者根本没上料 
			//空车，不需要定线，直接走路由
			toRoute(agv, dot, station.getDotno(),0,"制绒下料","",false);
		} else if (amount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty) {
			// 上满下空 
			 
			//空花篮没有定线
			toRoute(agv, dot, station.getDotno(),0,"制绒下料","",false);
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	/**
	 * 碱抛下料。如果空车，走路由，如果下满上空，还是走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveJPX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车，啥也没做
			toRoute(agv, dot, station.getDotno(),0,"","",false);

		} else if (amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
		 
			  String nextProcess="POPAID上料";
				StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
				 
			  if (gbean == null) {  
				    boolean tocache=iStation.findCache(CacheStationEnumType.JP	,agv.getAgvId(), true,true);
					if(tocache) {
						//可以去缓存
						RoadDao road = iRoad.getTransRoad(dot.getDotno(),CacheStationEnumType.JP.getDotno());
						if (road != null && road.getId() > 0) {  
							toStation(agv, dot, station.getDotno(),nextProcess, CacheStationEnumType.JP.getName() ,station.getSpecialline(),road.getId());
						} else {
							MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno(),CacheStationEnumType.JP.getDotno())); 
						}  
						
					}else {
						MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
					} 
 
			  } else { // 锁目标机台，然后走路由 
				  toRoute(agv,  dot, station.getDotno(),station.getSpecialline(),nextProcess,gbean.getStationname(),false);
			 }
			  
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	/**
	 * 碱抛上料，不是空车不准动
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveJPS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			 
			//没有空车，没有定线
			toRoute(agv, dot, station.getDotno(),0,"碱抛下料","",false);
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	
	
	/**
	 * POPAID比较特殊，只允许空车跑
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leavePOPAID(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) { 
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			if(agv.getApplyTransType() ==TransDirectionEnum.OnlyIn ) {
				// ************** 进料模式 ***********
				//进料后空车，离开了POPAID，直接去下道工序
				 
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			}else {
				//************** 出料模式 ************
				//没有出料，也去下一个
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			}
		}else if(amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
			if(agv.getApplyTransType() ==TransDirectionEnum.OnlyOut ) {
				// ******************* 出料模式 ***********
				//出料
				 
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			}else {
				//***************** 进料模式  **************
				MsgCore.sendMsg(agv.getAgvId(), "POPAID没上料，不能移动");
			} 
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "POPAID工序只有空车才能移动");
		}
	}

	/**
	 * 退火上料。如果是空车，找退火下料或者走路由，否则 不能动
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveTHS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			 
			String nextProcess = "退火下料";
			// 没有线别，找机台
			StationGroupDao gbean =iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);
			if (gbean == null) {
				// 没有可用的下料机台，走路由
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			} else {
				// 锁目标  
				RoadDao road = iRoad.getTransRoad(479, gbean.getDotno());
				if (road != null && road.getId() > 0) { 
					//有特殊路线，空车定线  
					toStation(agv, dot, station.getDotno(),nextProcess, gbean.getStationname(),gbean.getSpecialline(),road.getId());
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 479,gbean.getDotno())); 
				} 
			}
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	/**
	 * 退火下料，空车和上空下满，都走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveTHX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车走路由，没有下料的
			//没有线别,去BOE下料
			toRoute(agv, dot, station.getDotno(),0,"BOE下料","",false);
		} else if (amount.getMountType() != AgvMountTypeEnum.UpEmptyAndDownFull) {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		} else {
			 

			String nextProcess = "BOE上料";
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
			
			if (gbean == null) {
				 boolean tocache=iStation.findCache(CacheStationEnumType.TH	,agv.getAgvId(), true,true);
					if(tocache) {
						//去390
						toDot(agv, 161, station.getDotno(), station.getSpecialline(), nextProcess, CacheStationEnumType.TH.getName(), true);  
					}else {
						MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
					}  
			} else {
				// 锁目标机台，然后走路由 
				//没有特殊路线，也没有定线
				toRoute(agv, dot, station.getDotno(),gbean.getSpecialline(),gbean.getProcess(),gbean.getStationname(),false);
			}
		}
	}

	/**
	 * 只有空车才能走
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveBOES(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			 
			//空车
			toRoute(agv, dot, station.getDotno(),0,"BOE下料","",false);
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	/**
	 * BOE下料，空车走路由，上空下满去ALD湿法
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveBOEX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 没传输 ,直接去路由
			toRoute(agv, dot, station.getDotno(),0,"","",false);
		} else if (amount.getMountType() != AgvMountTypeEnum.UpEmptyAndDownFull) {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		} else {
			 
			String nextProcess = "ALD湿法"; 
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
			
			if(gbean==null) { 
				//TODO 找缓存机
				MsgCore.sendMsg(agv.getAgvId(), nextProcess+"没有可用的机台，暂时不能移动");
			}else {
				 //去451  
				toRoute(agv, dot, station.getDotno(),gbean.getSpecialline(),gbean.getProcess(),gbean.getStationname(),false); 
			}
			 
		}
	}

	/**
	 * 背膜上料，如果是空车或者没有机台，走路由。否则不动
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveBMS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) { 
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 传输完成，空车走路由
		 
			
			String nextProcess = "背膜下料";
			// 没有线别
			StationGroupDao gbean =iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), nextProcess, true);
			if (gbean == null) {
				// 没有可用的下料机台，走路由
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			} else {
				// 给路线 
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					//有特殊路线，空车定线  
					toStation(agv, dot, station.getDotno(),nextProcess, gbean.getStationname(),gbean.getSpecialline(),road.getId());
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 475,gbean.getDotno())); 
				} 
			}
			 
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		 
		}
	}

	/**
	 * 背膜下料，只要空车或者上空下满，就走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveBMX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车走路由
			toRoute(agv, dot, station.getDotno(),0,"","",false);
		} else if (amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) { 
			// 直接走路由，不判断丝网有没有机台
			 
			//找丝网
			String nextProcess = "丝网";
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
			
			if (gbean != null) { 
				//有机台，锁定， 没有特殊路线， 有定线
				//走路由去476
				toRoute(agv, dot, station.getDotno(),gbean.getSpecialline(),gbean.getProcess(),gbean.getStationname(),false);
			} else {
				// 没有可用的下料机台
			    MsgCore.sendMsg(agv.getAgvId(), String.format("%s没有可用的机台，等待中", nextProcess));
			} 
		}else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		}
	}

	/**
	 * 正膜上料，空车或者正膜下料没空位，走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveZMS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 传输完成 
			 
			List<String> processList=Arrays.asList("正膜下料","背膜下料");
		  
			StationGroupDao gbean =iStation.fetchNextOutStation(station.getDotno(), agv.getAgvId(), processList, true);
			if (gbean == null) {
				//  没有机台，走路由
				toRoute(agv, dot, station.getDotno(),0,"","",false);
			} else {
				// 锁目标机台，然后走路由
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					//有特殊路线，空车定线  
					toStation  (agv, dot, station.getDotno(),gbean.getProcess(), gbean.getStationname(),gbean.getSpecialline(),road.getId());
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 475,gbean.getDotno())); 
				} 
			} 
		} else {  
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动"); 
		}
	}

	/**
	 * 正膜下料，空车走路由， 上空下满找背膜上料
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveZMX(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车走路由
			toRoute(agv, dot, station.getDotno(),0,"","",false);
		} else if (amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
			 
			String nextProcess = "背膜上料";
			// 注意定线
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
			
			if (gbean == null) {
				// 没有可用的下料机台不移动
				//TODO 找缓存机
				MsgCore.sendMsg(agv.getAgvId(), nextProcess+"没有可用的机台，暂时不能移动");
			} else {
				// 锁目标机台，然后走路由
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					//有特殊路线，空车定线  
					toStation  (agv, dot, station.getDotno(),nextProcess, gbean.getStationname(),gbean.getSpecialline(),road.getId());
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 475,gbean.getDotno())); 
				} 
			}
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
			 
		}
	}

	/**
	 * 丝网，空车直接返空，上满下空才能走，才能去ALD干法
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveSW(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) { 
		//从473点开始的路线
		int startdot=473;
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty || amount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty) {
			// 空车走路由(只完成上料）
			 
			 
			String nextProcess = "ALD干法";
			StationGroupDao gbean =iStation.fetchNextOutStation(dot.getDotno(), agv.getAgvId(), nextProcess, true);
			
			if (gbean == null) {
				// 没有可用的下料机台，不动
				if(amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
					toRoute(agv, dot, station.getDotno(),0,"","",false);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), nextProcess+"没有可用的机台，暂时不能移动");
				}
			} else {
				RoadDao road = iRoad.getTransRoad(startdot, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					//有特殊路线 
					toStation (agv, dot, station.getDotno(),nextProcess, gbean.getStationname(),gbean.getSpecialline(),road.getId());
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 473,gbean.getDotno())); 
				} 
			} 
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动"); 
		}
		
	}

	/**
	 * ALD湿法，空车走路由，上满下空也走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveALDS(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车走路由（只上料）
		 
			//没有特殊路线，也没有定线
			toRoute(agv, dot, station.getDotno(),0,"","",false);
		} else if (amount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty) { 
		 
			//直接离开机台
			 toRoute(agv, dot, station.getDotno(), 0,"","",false);
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "ALD湿法装料情况不对，不能移动");
		}
	}

	/**
	 * ALD干法，空车走路由，上空下满也走路由
	 * 
	 * @param agv
	 * @param amount
	 * @param transDotno
	 * @param dot
	 */
	void leaveALDG(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车走路由
			 
			toRoute(agv, dot, station.getDotno(),0,"","",false);

		} else if (amount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
			 
			//找正膜上料口
			String nextProcess = "正膜上料";
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, station.getSpecialline(), true);
			
			if (gbean == null) {
				toRoute(agv, dot, station.getDotno(),station.getSpecialline(),"","",false);
			} else { 
				 toRoute(agv, dot, station.getDotno(), gbean.getSpecialline(),gbean.getProcess(),gbean.getStationname(),false);
			}  
		} else {
			MsgCore.sendMsg(agv.getAgvId(), "ALD干法装料情况不对，不能移动");
		}
	}
	
	
	void leaveCB(AgvBean agv, AgvMountBean amount, StationBean station, DotBean dot) {
		  if (amount.getMountType() != AgvMountTypeEnum.UpFullAndDownEmpty) {
			MsgCore.sendMsg(agv.getAgvId(), "装料情况不对，不能移动");
		} else {
		 
			String nextProcess = "发料上料";
			StationGroupDao gbean =iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), nextProcess, 0, true);
			

			if (gbean == null) {
				MsgCore.sendMsg(agv.getAgvId(), "发料上料没有可用的机台");
			} else {
				//没有特殊路线，也没有定线 
				RoadDao road    =iRoad.getTransRoad(201, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					StationCmdMap.clearState(station.getDotno());
					// 空车解锁所有的
					iStation.unlockStationForDot(station.getDotno(), agv.getAgvId());
 
					// 行驶
					agv.setNextProcess("");
					agv.setNexttStationName("");
					agv.setNextRoad(0);
					agv.setSpecialline(0);
					agv.setFinishTrans(false);
					agv.setUpHasTrans(false);
					agv.setDownHasTrans(false); 
					iAgv.saveCache(agv);
  
					 // 小车跑路
					iAgvEvent.toRoad(agv, road.getId());
					 
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d到%d缺少路线????", 201, gbean.getDotno()));
				}
			}
			
			 
		}
	}
	 

}
